/**
 * 
 */
package weibo4j.crawler;

import java.sql.Connection;
import java.util.Date;
import java.util.List;
import weibo4j.Status;
import weibo4j.Weibo;
import weibo4j.database.DataBaseUtil;

/**
 * @author longrui
 * 
 */

public class StatusCrawler extends CrawlerBase {
	private QueueBase queue4Status;
	private List<Status> statuses;
	private Connection statusConn;
	private CrawlerBase mainCrawler;

	/**
	 * @param weibo
	 */
	public StatusCrawler(Weibo weibo) {
		super(weibo);
		// TODO Auto-generated constructor stub
	}

	public void init(CrawlerBase mainCrawler, QueueBase queue4Status) {
		this.queue4Status = queue4Status;
		statusConn = DataBaseUtil.getDefaultConnection();
		this.mainCrawler = mainCrawler;
	}
	
	public QueueBase getQueueBaseType(){
		return queue4Status;
	}

	public void run() {
		StatusQueueRecord rec = null;
		try {
			System.out.println("Status crawler is starting ...");			

			while (true) {				
				((UserInfoCrawler) mainCrawler).prepare(this);
				System.out.println("working... Status queue's size:"
							+ queue4Status.getLength());
				rec = (StatusQueueRecord) queue4Status.poll();
				statuses = super.getRetweetedStatusByStatusId(rec.statusId);

				// save comments into database;
				System.out.println("statuses count: " + statuses.size());
				int i = 0;
				for (Status status : statuses) {
					//DataBaseUtil.insert(statusConn, "statuses",	Status.getFieldString(), Status.getEmptyValueString(),
					//		status.getRecord("utf-8"));
					DataBaseUtil.executeStatement(statusConn, status.getStatusSQLString("utf-8"));
					//System.out.println("insert statuses by status thread: " + i++);
					//status.toString();
					
					queue4Status.add(new StatusQueueRecord(status.getId(), myDateTimeFormat.format(new Date())));;
				}
				adjustFreq();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
